package com.lucenly.card.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lucenly.card.R;
import com.lucenly.card.app.MyApplication;
import com.lucenly.card.bean.ShareBean;
import com.lucenly.card.bean.UserInfo;
import com.lucenly.card.utils.Util;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

public class ShareDialog extends Dialog{

    public ShareDialog(@NonNull final Context context, final ShareBean shareBean) {
        super(context, R.style.alert_dialog);
        setContentView(R.layout.dialog_share);
        initView(context);
        ImageView iv_wx = findViewById(R.id.iv_wx);
        ImageView iv_zfb = findViewById(R.id.iv_zfb);
        iv_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        UserInfo user = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
                        WXWebpageObject webpageObject = new WXWebpageObject();
                        webpageObject.webpageUrl = shareBean.shareUrl+user.getId();
                        WXMediaMessage mediaMessage = new WXMediaMessage(webpageObject);
                        mediaMessage.title = shareBean.shareTitle;
                        mediaMessage.description=shareBean.shareDesc;
//                File file = new File(context.getExternalCacheDir()+"/download/share_logo.png");
//                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                        Bitmap bitmap = null ;
                        try {
                            bitmap = BitmapFactory.decodeStream(new URL(shareBean.shareLogo).openStream());
                            byte[] bytes = Util.bmpToByteArray(bitmap, true);
                            mediaMessage.thumbData = bytes;
                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                            req.scene = SendMessageToWX.Req.WXSceneSession;
                            req.message = mediaMessage;
                            req.transaction = "share";
                            MyApplication.getInstance().getApi().sendReq(req);
                            dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
        iv_zfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        UserInfo user = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
                        WXWebpageObject webpageObject = new WXWebpageObject();
                        webpageObject.webpageUrl = shareBean.shareUrl+user.getId();
                        WXMediaMessage mediaMessage = new WXMediaMessage(webpageObject);
                        mediaMessage.title = shareBean.shareTitle;
                        mediaMessage.description=shareBean.shareDesc;
//                File file = new File(context.getExternalCacheDir()+"/download/share_logo.png");
//                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                        Bitmap bitmap = null ;
                        try {
                            bitmap = BitmapFactory.decodeStream(new URL(shareBean.shareLogo).openStream());
                            byte[] bytes = Util.bmpToByteArray(bitmap, true);
                            mediaMessage.thumbData = bytes;
                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                            req.scene = SendMessageToWX.Req.WXSceneTimeline;
                            req.message = mediaMessage;
                            req.transaction = "share";
                            MyApplication.getInstance().getApi().sendReq(req);
                            dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }
    public static byte[] toByteArray(File photo) throws IOException{
        FileInputStream fis = new FileInputStream(photo);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = bis.read();

        while(c!=-1){
            baos.write(c);
            c=bis.read();
        }
        bis.close();
        byte[] rtn = baos.toByteArray();
        baos.close();

        return rtn;
    }

    private void initView(Context context) {
        LinearLayout lLayout_bg = (LinearLayout) findViewById(R.id.lLayout_bg);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        setCanceledOnTouchOutside(false);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        setCanceledOnTouchOutside(true);
    }
}
